﻿using System;
using Glue;
using Xunit;

namespace GluePerformanceTesting
{
    public class NestedMapping
    {
        private readonly PerformanceMeasure measurer;

        private class GuiPerson { public GuiAddress Address { get; set; } }
        public class GuiAddress { public String City { get; set; } }
        private class DomainPerson { public DomainAddress Address { get; set; } }
        public class DomainAddress { public String City { get; set;}}

        public NestedMapping()
        {
            measurer = PerformanceMeasure.GetInstance();
        }

        [Fact]
        public void Run_end_to_end()
        {
            var domainPerson = new DomainPerson {Address = new DomainAddress{City = "Trondheim"}};
            measurer.RunVeryLong("Nested mapping, end to end", () =>
            {
                var mapping = new Mapping<DomainPerson, GuiPerson>();
                var nestedMapping = new Mapping<DomainAddress, GuiAddress>();
                nestedMapping.Relate(domain => domain.City, gui=>gui.City);
                mapping.Relate(domain=>domain.Address,gui=>gui.Address,nestedMapping);
                mapping.Map(domainPerson, new GuiPerson{Address = new GuiAddress()});
            });
        }

        [Fact]
        public void Run_mapping_only()
        {
            var mapping = new Mapping<DomainPerson, GuiPerson>();
            var nestedMapping = new Mapping<DomainAddress, GuiAddress>();
            nestedMapping.Relate(domain => domain.City, gui => gui.City);
            mapping.Relate(domain => domain.Address, gui => gui.Address, nestedMapping);
            var domainPerson = new DomainPerson { Address = new DomainAddress { City = "Trondheim" } };
            measurer.Run("Nested mapping, mapping only", () => mapping.Map(domainPerson, new GuiPerson { Address = new GuiAddress() }));
        }
    }
}
